﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility.Json;

namespace Utility.Pay.Alipay.App
{
    public class AlipayApp
    {


        public static string sign(AliPay pay)
        {
            var strRetData = "";
            try
            {
                if (pay == null)
                {
                    strRetData = JsonHelper.GetJsonString("false", "-1", "生成订单出错", "");
                }


                Dictionary<string, string> keyvalues = new Dictionary<string, string>();


                keyvalues.Add("app_id", Config.app_id);
                keyvalues.Add("method", Config.method);
                keyvalues.Add("format", Config.format);
                keyvalues.Add("charset", Config.charset);
                keyvalues.Add("sign_type", Config.sign_type);
                //keyvalues.Add("sign", Config.private_key);
                keyvalues.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                keyvalues.Add("version", Config.version);
                keyvalues.Add("notify_url", pay.notifyUrl);

                var biz_content = new
                {
                    subject = pay.subject,
                    out_trade_no = pay.tradeNo,
                    total_amount = pay.price,
                    product_code = "QUICK_MSECURITY_PAY",

                };

                keyvalues.Add("biz_content", JsonHelper.ObjectToJson(biz_content));

                var strJson = Core.CreateLinkStringUrlencode(keyvalues, Encoding.UTF8);

                var signstrJson = RSAFromPkcs8.sign(strJson, Config.private_key, Config.charset);

                strRetData = JsonHelper.GetJsonString("true", "1", "生成订单成功", signstrJson);



            }
            catch (Exception ex)
            {
                strRetData = JsonHelper.GetJsonString("false", "-1", "系统异常", "");
            }

            return strRetData;
        }


    }
}
